package com.lizhi.note.entity;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

@Entity
@Table(name = "notepage_history")
@EntityListeners(AuditingEntityListener.class)
@Data
@AllArgsConstructor
@NoArgsConstructor
@SQLDelete(sql = "UPDATE notepage_history SET deleted = id WHERE id = ?")
@Where(clause = "deleted = 0")
public class NotepageHistoryEntity extends BaseEntity{

    @NotBlank
    @Size(max = 20)
    private String title;

    private String summary;

    @Column(columnDefinition = "longtext")
    private String content;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "notebook_id", foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
    private NotebookEntity notebook;

    private Long notePageId;

    public NotepageHistoryEntity(NotepageEntity notepageEntity){
        this.title = notepageEntity.getTitle();
        this.summary = notepageEntity.getSummary();
        this.content = notepageEntity.getContent();
        this.notePageId = notepageEntity.getId();

        NotebookEntity notebookEntity = new NotebookEntity();
        this.notebook = notepageEntity.getNotebook();
    }

    public static void main(String[] args) {
        NotepageEntity notepageEntity = new NotepageEntity();
        notepageEntity.setTitle("9999");

        NotepageHistoryEntity historyEntity = new NotepageHistoryEntity(notepageEntity);

        System.out.println(historyEntity);

        notepageEntity.setTitle("1111111");
        System.out.println(historyEntity);
        System.out.println(notepageEntity);
    }
}
